package com.bsoft.gol.editor.service.impl;

import ctd.controller.exception.ControllerException;
import ctd.util.BeanUtils;
import com.bsoft.gol.editor.dao.EditorFormQrDAO;
import com.bsoft.gol.editor.editor.base.EditorFormQr;
import com.bsoft.gol.editor.editor.qo.AddEditorFormQrQo;
import com.bsoft.gol.editor.editor.qo.QueryEditorFormQrQo;
import com.bsoft.gol.editor.editor.qo.RemoveEditorFormQrQo;
import hcn.base.OrgMinor;
import hcn.bean.QueryAuthorityOrgListQo;
import hcn.bean.vo.QueryAuthorityOrgListVo;
import hcn.util.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.atag.dao.model.PageInfo;
import com.bsoft.gol.editor.service.EditorFormQrApi;
import service.rpc.OrganizationInfoIntf;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: Myproject
 * @description: 表单二维码
 * @author: zangsf
 * @create: 2022-12-17 13:21
 **/
@Service("editorFormQrApi")
public class EditorFormQrApiImpl implements EditorFormQrApi {
    private static final Logger logger = LoggerFactory.getLogger(EditorFormQrApiImpl.class);
    @Autowired
    private EditorFormQrDAO editorFormQrDAO;
    @Autowired
    private OrganizationInfoIntf organizationInfoIntf;

    @Value("${qr_code_image_org}")
    private String qrCodeImageOrg;
    @Value("${qr_code_image_doct}")
    private String qrCodeImageDoct;
    /* *
     * @description :查询表单列表
     * @author      :zangsf
     * @date        :2022-12-17 14:06
     * @param       :[qo]
     * @return      :hcn.util.BaseResponse<org.atag.dao.model.PageInfo<com.bsoft.gol.editor.editor.base.EditorFormQr>>
     */
    @Override
    public BaseResponse<PageInfo<EditorFormQr>> list(QueryEditorFormQrQo req) {
        BaseResponse baseResponse = new BaseResponse();
        PageInfo<EditorFormQr> vo = editorFormQrDAO.queryEditorFormQr(req);
        baseResponse.setData(vo);
        return baseResponse;
    }

    /* *
     * @description :删除表单
     * @author      :zangsf
     * @date        :2022-12-17 14:07
     * @param       :[qo]
     * @return      :hcn.util.BaseResponse
     */
    @Override
    public BaseResponse remove(RemoveEditorFormQrQo qo) {
        BaseResponse baseResponse = new BaseResponse();
        if (CommonUtils.isNotEmpty(qo.getIdList())){
            for (String s : qo.getIdList()) {
                editorFormQrDAO.remove(s);
            }
        }else {
            baseResponse.setCode(500);
            baseResponse.setMessage("未传入主键id");
        }
        return baseResponse;
    }

    /* *
     * @description :更新表单二维码
     * @author      :zangsf
     * @date        :2022-12-17 14:13
     * @param       :[qo]
     * @return      :hcn.util.BaseResponse
     */
    @Override
    public BaseResponse update(EditorFormQr qo) {
        BaseResponse baseResponse = new BaseResponse();
        if (CommonUtils.isNotEmpty(qo.getEditorQrId())&&CommonUtils.isNotEmpty(qo.getStatus())){
            editorFormQrDAO.updateStatus(qo.getEditorQrId(),qo.getStatus());
        }else {  baseResponse.setCode(500);
            baseResponse.setMessage("未传入修改状态/主键");
        }
        return baseResponse;
    }

    /* *
     * @description :添加表单二维码
     * @author      :zangsf
     * @date        :2022-12-17 14:22
     * @param       :[qo]
     * @return      :hcn.util.BaseResponse
     */
    @Override
    public BaseResponse add(List<AddEditorFormQrQo> qos) {
        BaseResponse baseResponse = new BaseResponse();
        if (CommonUtils.isNotEmpty(qos)){
            for (AddEditorFormQrQo qo : qos) {
                EditorFormQr add = new EditorFormQr();
                OrgMinor orgMinor = null;
                if (2==qo.getObjectType()){
                    try {
                        orgMinor = organizationInfoIntf.getOrgMinor(qo.getOrgId());
                    } catch (ControllerException e) {
                        e.printStackTrace();
                    }
                }
                BeanUtils.copy(qo,add);
                if (CommonUtils.isNotEmpty(qo.getListObject())){
                    for (AddEditorFormQrQo.AddQr addQr : qo.getListObject()) {
                        add.setObjectId(addQr.getObjectId());
                        add.setObjectName(addQr.getObjectName());
                        //这里判断是否存在如果存在则不需要添加
                        EditorFormQr status = editorFormQrDAO.getStatus(add.getObjectType(), add.getOrgId(), add.getQuestionnaireId(), add.getObjectId());
                        if (CommonUtils.isEmpty(status)){
                            add.setEditorQrId(UUID.randomUUID().toString().replace("-",""));
                            add.setCreateTime(new Date());
                            add.setObjectLevel(addQr.getObjectLevel());
                            if (2==qo.getObjectType()){
                                if (CommonUtils.isNotEmpty(orgMinor)){
                                    add.setObjectPhoto(orgMinor.getAvatarField());
                                }else {
                                    add.setObjectPhoto(addQr.getObjectPhoto());
                                }
                                if (CommonUtils.isEmpty(add.getObjectPhoto())){
                                    add.setObjectPhoto(qrCodeImageOrg);
                                }
                            }else {
                                if (CommonUtils.isNotEmpty(addQr.getObjectPhoto())){
                                    add.setObjectPhoto(addQr.getObjectPhoto());
                                }else {
                                    add.setObjectPhoto(qrCodeImageDoct);
                                }
                            }
                            editorFormQrDAO.save(add);
                        }else {
                            if (2==qo.getObjectType()){
                                if (CommonUtils.isNotEmpty(orgMinor)){
                                    status.setObjectPhoto(orgMinor.getAvatarField());
                                }else {
                                    status.setObjectPhoto(addQr.getObjectPhoto());
                                }
                                if (CommonUtils.isEmpty(status.getObjectPhoto())){
                                    status.setObjectPhoto(qrCodeImageOrg);
                                }
                            }else {
                                if (CommonUtils.isNotEmpty(addQr.getObjectPhoto())){
                                    status.setObjectPhoto(addQr.getObjectPhoto());
                                }else {
                                    status.setObjectPhoto(qrCodeImageDoct);
                                }
                            }
                            editorFormQrDAO.update(status);
                        }
                    }
                }
            }
        }
        return baseResponse;
    }

    @Override
    public  List<String>  export(RemoveEditorFormQrQo qos) {
        List<String> qrUrl = editorFormQrDAO.findQrUrl(qos.getIdList());
        return qrUrl;
    }
}
